#include "StdAfx.h"

InputFileHandler::InputFileHandler(void) { }

InputFileHandler::~InputFileHandler(void) { }


void InputFileHandler::ReadQuotations(string filePath, Stock *stock)
{
	string line;
	this->_inputFile.open(filePath.c_str(), ifstream::in);

	if( this->_inputFile.is_open() )
	{
		while ( !this->_inputFile.eof() ) 
		{
			getline (this->_inputFile, line);
			
			if (line != "")
				stock->AddQuotation( ParseLine(line) );
		}
		this->_inputFile.close();
	}
}

Quotation InputFileHandler::ParseLine(string line)
{
	//line = dd/mm/yyyy;hh:mm:ss;Open;High;Low;Close;Volume
	char *aux, *lin;
	Quotation quotAux;
	
	lin = new char[line.size()+1];
	strcpy(lin, line.c_str());
		
	aux = strtok(lin, "/");
	quotAux._DateTime.Date.Day = atoi(aux);

	aux = strtok(NULL, "/");
	quotAux._DateTime.Date.Month = atoi(aux);

	aux = strtok(NULL, ";");
	quotAux._DateTime.Date.Year = atoi(aux) - 1960;
		
	aux = strtok(NULL, ":");
	quotAux._DateTime.Date.Hour = atoi(aux);
		
	aux = strtok(NULL, ":");
	quotAux._DateTime.Date.Minute = atoi(aux);
	
	aux = strtok(NULL, ";");
	quotAux._DateTime.Date.Second = atoi(aux);

	aux = strtok(NULL, ";");
	quotAux.Prices[OPEN] = atof(aux);

	aux = strtok(NULL, ";");
	quotAux.Prices[HIGH] = atof(aux);

	aux = strtok(NULL, ";");
	quotAux.Prices[LOW] = atof(aux); 

	aux = strtok(NULL, ";");
	quotAux.Prices[CLOSE] = atof(aux);

	aux = strtok(NULL, ";");
	quotAux.Volume = atoi(aux);
	
	delete[] lin;
	return quotAux;
}